package main

import (
	"bufio"
	"fmt"
	"os"
)

func CountLines(f *os.File, countLines map[string]int) {
	input := bufio.NewScanner(f)
	for input.Scan() {
		line := input.Text()
		if line == "exit" {
			break
		}
		countLines[line]++
	}
}

func main() {
	countLines := make(map[string]int)
	files := os.Args[1:]
	if len(files) == 0 {
		CountLines(os.Stdin, countLines)
	} else {
		for _, arg := range files {
			f, err := os.Open(arg)
			if err != nil {
				fmt.Fprintf(os.Stderr, "udp2: %v\n", err)
				continue
			}
			CountLines(f, countLines)
			f.Close()
		}
	}
	fmt.Println(countLines)
	for line, n := range countLines {
		fmt.Println(line, n)
	}
}
